package com.elinkway.infinitemovies.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.PlayRecord;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.MoviesConstant;

import java.util.ArrayList;
import java.util.List;

public class DownLoadPlayRecordDao extends BaseDao<PlayRecord> {

    private static final String TABLE_NAME = "download_playrecord";
    private static final String FIELD_ID = "id";
    private static final String FIELD_NAME = "name";
    private static final String FIELD_TIME = "time";
    private static final String FIELD_URL = "url";
    private static final String FIELD_AID = "aid";
    private static final String FIELD_EPISODE = "episode";
    private static final String FIELD_VT = "vt";
    private static final String FIELD_CATEGORYNAME = "category_name";
    private static final String FIELD_SEEKHISTORY = "seekHistory";
    private static final String FIELD_PORDER = "porder";
    private static final String FIELD_SITE = "site";
    private static final String FIELD_REQUEST_TYPE = "request_type";


    private static DownLoadPlayRecordDao mInstance;

    private DownLoadPlayRecordDao(Context context) {
        super(context);
    }

    public static DownLoadPlayRecordDao getInstance(Context context) {
        if (null == mInstance) {
            mInstance = new DownLoadPlayRecordDao(context);
        }
        return mInstance;
    }

    @Override
    public synchronized void delete(final String aid) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                SQLiteDatabase db = safelyGetDataBase();
                LogUtils.e("save", String.format("delete from %s where %s='%s'", TABLE_NAME, FIELD_AID, aid));
                db.execSQL(String.format("delete from %s where %s='%s'", TABLE_NAME, FIELD_AID, aid));
                safelyCloseDataBase();
            }

        });
    }

    @Override
    public synchronized void delAll() {

    }

    public synchronized List<PlayRecord> getAll() {
        List<PlayRecord> list = new ArrayList<PlayRecord>();
        SQLiteDatabase db = safelyGetDataBase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " order by id DESC", null);
        while (cursor.moveToNext()) {
            PlayRecord record = new PlayRecord();
            record.setAid(cursor.getString(1));
            record.setName(cursor.getString(2));
            record.setTime(cursor.getLong(4));
            record.setUrl(cursor.getString(3));
            record.setEpisodeName(cursor.getString(5));
            record.setVt(cursor.getString(6));
            record.setCategoryName(cursor.getString(7));
            record.setSeekHistory(cursor.getLong(8));
            record.setPorder(cursor.getString(9));
            record.setSite(cursor.getString(10));
            record.setRequestType(cursor.getString(11));
            list.add(record);
        }
        safelyCloseDataBase();
        cursor.close();
        return list;
    }

    @Override
    public synchronized void save(final PlayRecord bean) {
        if (TextUtils.isEmpty(bean.getAid())) {
            return;
        }

        doInBackground(new Runnable() {

            @Override
            public void run() {
                SQLiteDatabase db = safelyGetDataBase();
                LogUtils.e("save", "saving " + bean.getAid());
                db.execSQL(String.format("delete from %s where %s='%s' and %s='%s'", TABLE_NAME, FIELD_AID, bean.getAid(), FIELD_PORDER, bean.getPorder()));
                Cursor cursor = db.rawQuery(String.format("Select * from %s;", TABLE_NAME), null);
                if (cursor.getCount() == MoviesConstant.PLAY_RECORD_MAXIMUM_SIZE) {
                    db.execSQL("delete from " + TABLE_NAME + " where " + FIELD_ID + "=(select min(" + FIELD_ID + ") from " + TABLE_NAME + ")");
                }
                ContentValues values = new ContentValues();
                values.put(FIELD_AID, bean.getAid());
                values.put(FIELD_NAME, bean.getName());
                values.put(FIELD_URL, bean.getUrl());
                values.put(FIELD_TIME, System.currentTimeMillis());
                values.put(FIELD_EPISODE, bean.getEpisode());
                values.put(FIELD_VT, bean.getVt());
                values.put(FIELD_CATEGORYNAME, bean.getCategoryName());
                values.put(FIELD_SEEKHISTORY, bean.getSeekHistory());
                values.put(FIELD_PORDER, bean.getPorder());
                values.put(FIELD_SITE, bean.getSite());
                values.put(FIELD_REQUEST_TYPE, bean.getRequestType());
                db.insert(TABLE_NAME, null, values);
                safelyCloseDataBase();
                cursor.close();
            }

        });
    }

    public synchronized PlayRecord getByAidAndPorder(String aid, String porder) {
        PlayRecord record = new PlayRecord();
        SQLiteDatabase db = safelyGetDataBase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where " + FIELD_AID + "='" + aid + "'" + " and " + FIELD_PORDER + "='" + porder + "'", null);
        if (cursor.getCount() == 0) {
            safelyCloseDataBase();
            cursor.close();
            return null;
        }
        cursor.moveToNext();
        record.setAid(cursor.getString(1));
        record.setName(cursor.getString(2));
        record.setTime(cursor.getLong(4));
        record.setUrl(cursor.getString(3));
        record.setEpisodeName(cursor.getString(5));
        record.setVt(cursor.getString(6));
        record.setCategoryName(cursor.getString(7));
        record.setSeekHistory(cursor.getLong(8));
        record.setPorder(cursor.getString(9));
        record.setSite(cursor.getString(10));
        record.setRequestType(cursor.getString(11));
        safelyCloseDataBase();
        cursor.close();
        return record;
    }


}
